package com.ruoyi.web.controller.repository;

import java.util.List;

import com.ruoyi.system.repository.domain.Member;
import com.ruoyi.system.repository.service.IMemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员Controller
 *
 * @author qiang
 * @date 2021-01-01
 */
@RestController
@RequestMapping("/repository/member")
public class MemberController extends BaseController {
    @Autowired
    private IMemberService memberService;

/**
 * 查询会员列表
 */
@PreAuthorize("@ss.hasPermi('repository:member:list')")
@GetMapping("/list")
        public TableDataInfo list(Member member) {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize("@ss.hasPermi('repository:member:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Member member) {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member. class);
        return util.exportExcel(list, "member");
    }

    /**
     * 获取会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('repository:member:query')")
    @GetMapping(value = "/{phone}")
    public AjaxResult getInfo(@PathVariable("phone") String phone) {
        return AjaxResult.success(memberService.selectMemberById(phone));
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('repository:member:add')")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member) {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改会员
     */
    @PreAuthorize("@ss.hasPermi('repository:member:edit')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member member) {
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('repository:member:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{phones}")
    public AjaxResult remove(@PathVariable String[] phones) {
        return toAjax(memberService.deleteMemberByIds(phones));
    }

    @GetMapping("public/logon")
    public String logon(Member user){
        return memberService.logon(user);
    }

    @GetMapping("public/login")
    public String login(Member user){
        String res= memberService.login(user);
        return res;
    }

    @GetMapping("public/findByPhone")
    public Member findByPhone(String phone){
        Member res= memberService.findByPhone(phone);
        return res;
    }

    @GetMapping("public/changeUser")
    public String changeUser(Member user){
        String res= memberService.changeUser(user);
        return res;
    }

    @GetMapping("public/findByPhoneAndPwd")
    public String findByPhoneAndPwd(Member user){
        String res= memberService.findByPhoneAndPwd(user);
        return res;
    }

    @GetMapping("public/changePwd")
    public String changePwd(Member user){
        String res= memberService.changePwd(user);
        return res;
    }

    @GetMapping("public/judge")
    public String judge(String openid){
        return memberService.judge(openid);
    }

    @GetMapping("public/addOpenid")
    public String addOpenid(String openid,String phone){
        String res= memberService.addOpenid(openid,phone);
        return res;
    }

}
